﻿using System;
using EmbeddedSharp.Scripting;
using EmbeddedSharp.Core;

namespace EmbeddedSharp.Scripting.Function.Context {

    /// <summary>
    /// Assign a new variable with a value
    /// </summary>
    [Function("assign", "Assign a new variable with a value", "a=10;", "a==10")]
    public class AssignNode : OperatorNode {

        protected IContext context = null;

        public AssignNode(IContext context)
            : base() {
            this.Value = "=";
            this.Precedence = 0;
            this.context = context;
        }
        
        public override object Evaluate() {
            string variableName
                = Left.Value;
            object value = Right.Evaluate();
            value = this.context.Assign(variableName, value);

            return value;
        }
    }
}
